package com.wxtt.android.activity;

import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;

import com.fast.dev.frame.ui.Bind;
import com.fast.dev.frame.utils.StringUtils;
import com.fast.dev.frame.utils.UIUtils;
import com.wxtt.android.R;
import com.wxtt.android.bean.SuggestBean;
import com.wxtt.android.common.CommonActivity;
import com.wxtt.android.config.ApiConfig;
import com.wxtt.android.listener.BaseListener;
import com.wxtt.android.view.HandlerHelper;

/**
 * 说明：意见反馈
 * <p/>
 * 作者：fanly
 * <p/>
 * 时间：2016/1/4 14:33
 * <p/>
 * 版本：verson 1.0
 */
public class SuggestActivity extends CommonActivity{

    @Bind(id = R.id.et_name)
    private EditText et_name;
    @Bind(id = R.id.et_phone)
    private EditText et_phone;
    @Bind(id = R.id.et_qq)
    private EditText et_qq;
    @Bind(id = R.id.et_suggest_text)
    private EditText et_suggest;

    private View sendView;

    @Override
    public int setRootView() {
        return R.layout.activity_suggest;
    }

    @Override
    protected boolean showToolbar() {
        return true;
    }

    @Override
    public int setToolbarTitle() {
        return R.string.navigation_item_suggest;
    }

    @Override
    protected void onCustomToolbar(View view, Toolbar toolbar) {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected int setMenu() {
        return R.menu.suggest_menu;
    }

    @Override
    protected void onMenuItemClick(int id) {
        super.onMenuItemClick(id);
        switch (id){
            case R.id.suggest_meun_send:
                if (sendView == null) sendView = findViewById(R.id.suggest_meun_send);
                String content = et_suggest.getText().toString();
                if (check(content)){
                    send(content);
                }
                break;
            default:
                break;
        }
    }

    /**
     * 说明：发送数据
     * @param content
     */
    private void send(String content){
        final SuggestBean bean = new SuggestBean();
        bean.setName(et_name.getText().toString());
        bean.setPhone(et_phone.getText().toString());
        bean.setQQ(et_qq.getText().toString());
        bean.setContent(content);
        showLoading(UIUtils.getString(R.string.str_loading_submit));
        sendView.setEnabled(false);
        HandlerHelper.postDelay(3000, new Runnable() {
            @Override
            public void run() {
                ApiConfig.sendSuggest(SuggestActivity.this, bean, new BaseListener() {

                    @Override
                    public void success() {
                        dismissLoading();
                        shortToast("感谢您的反馈!");
                        HandlerHelper.postDelay(1000, new Runnable() {
                            @Override
                            public void run() {
                                finish();
                            }
                        });
                    }

                    @Override
                    public void fail(int code, String error) {
                        dismissLoading();
                        if (code == 9016) {
                            error = "网络连接错误!";
                        }
                        shortToast(error);
                        sendView.setEnabled(true);
                    }
                });
            }
        });
    }

    /**
     * 说明：检查意见内容
     * @return
     */
    private boolean check(String content){
        if (StringUtils.isEmpty(content)){
            shortToast("请输入内容");
            return false;
        }else {
            return true;
        }
    }
}
